package com.jyw.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.EnumUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyw.system.domain.SysMessage;
import com.jyw.system.domain.dto.SysMessageAddDto;
import com.jyw.system.domain.dto.SysMessageEditDto;
import com.jyw.system.domain.dto.SysMessagePageDto;
import com.jyw.system.domain.vo.SysMessageVo;
import com.jyw.system.enums.MessageEnum;
import com.jyw.system.enums.MessageTypeEnum;
import com.jyw.system.mapper.SysMessageMapper;
import com.jyw.system.service.ISysMessageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统消息Service业务层处理
 *
 * @author zhangcheng
 * @date 2024-01-23
 */
@Service
public class SysMessageServiceImpl extends ServiceImpl<SysMessageMapper, SysMessage> implements ISysMessageService {
    @Resource
    private SysMessageMapper sysMessageMapper;

    private static final String SYS_RECEIVER="系统消息";

    @Override
    public void sendMessage(SysMessageAddDto addDto) {
        SysMessage sysMessage=new SysMessage();
        sysMessage.setMessageType(addDto.getMessageType());
        sysMessage.setMessageContent(addDto.getMessageContent());
        sysMessage.setSender(addDto.getSender());
        sysMessage.setReceiver(addDto.getReceiver());
        sysMessage.setReading(0);
        sysMessage.setSendingTime(LocalDateTime.now());
        this.save(sysMessage);
    }

    @Override
    public void sendSysMessage(Integer messageType, Long receiver, String messageContent) {
        SysMessage sysMessage=new SysMessage();
        sysMessage.setType(MessageEnum.PC.getValue());
        sysMessage.setMessageType(messageType);
        sysMessage.setMessageContent(messageContent);
        sysMessage.setSender(SYS_RECEIVER);
        sysMessage.setReceiver(receiver);
        sysMessage.setReading(0);
        sysMessage.setSendingTime(LocalDateTime.now());
        this.save(sysMessage);
    }

    @Override
    public void sendBatchSysMessage(Integer messageType, List<Long> receivers, String messageContent) {
        if(CollUtil.isNotEmpty(receivers)){
            List<SysMessage> messages=new ArrayList<>();
            receivers.forEach(receiver->{
                SysMessage sysMessage=new SysMessage();
                sysMessage.setType(MessageEnum.PC.getValue());
                sysMessage.setMessageType(messageType);
                sysMessage.setMessageContent(messageContent);
                sysMessage.setSender(SYS_RECEIVER);
                sysMessage.setReceiver(receiver);
                sysMessage.setReading(0);
                sysMessage.setSendingTime(LocalDateTime.now());
                messages.add(sysMessage);
            });
            if(CollUtil.isNotEmpty(messages)){
                this.saveBatch(messages);
            }
        }
    }

    @Override
    public List<SysMessageVo> pageList(SysMessagePageDto pageDto) {
        List<SysMessageVo> list=sysMessageMapper.pageList(pageDto);
        if(CollUtil.isNotEmpty(list)){
            list.forEach(record->{
                covertEnum(record);
            });
        }
        return list;
    }

    @Override
    public Long getCount(Long userId) {
        return sysMessageMapper.getCount(userId);
    }

    private void covertEnum(SysMessageVo record){
        // 消息类型
        if (record.getMessageType() != null) {
            MessageTypeEnum stateEnum = EnumUtil.getBy(MessageTypeEnum.class, (a -> a.getValue().equals(record.getMessageType())));
            if (stateEnum != null) {
                record.setMessageTypeName(stateEnum.getName());
            }
        }
    }

    @Override
    public SysMessageVo selectSysMessageById(Long id) {
        SysMessageVo record=sysMessageMapper.selectSysMessageById(id);
        if(record!=null){
            covertEnum(record);
        }
        return record;
    }

    @Override
    public void edit(SysMessageEditDto editDto) {
        SysMessage sysMessage=this.getById(editDto.getId());
        if(sysMessage!=null){
            BeanUtil.copyProperties(editDto, sysMessage);
            sysMessage.setReadingTime(LocalDateTime.now());
            this.updateById(sysMessage);
        }
    }

    @Override
    public void batchReading(Long[] ids) {
        if(ids.length>0){
            this.baseMapper.updateBatch(ids);
        }
    }

    @Override
    public void allReading(Long receiver) {
        List<Long> idList=this.baseMapper.getIdsByReceiver(receiver);
        if(CollUtil.isNotEmpty(idList)){
            Long[] ids=idList.stream().toArray(Long[]::new);
            this.baseMapper.updateBatch(ids);
        }
    }
}
